package com.hzo.great.leisuregreat.views;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzo.great.leisuregreat.R;
import com.hzo.great.leisuregreat.base.BaseActivity;
import com.hzo.great.leisuregreat.fragments.HomeFragment;
import com.hzo.great.leisuregreat.fragments.MineFragment;

public class MainActivity extends BaseActivity {
    private ImageView imgHome;
    private TextView txtHome;
    private ImageView imgWode;
    private LinearLayout layoutHome;
    private LinearLayout layoutWode;

    private HomeFragment homeFragment = new HomeFragment();
    private MineFragment mineFragment = new MineFragment();
    Fragment currentFragment = new Fragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fullScreen(this, true);
        setContentView(R.layout.activity_main);
        initView();
        switchFragment(homeFragment).commit();
    }

    public void initView() {
        imgHome = findViewById(R.id.img_home);
        txtHome = findViewById(R.id.txt_home);
        imgWode = findViewById(R.id.img_wode);
        layoutHome = findViewById(R.id.layout_home);
        layoutWode = findViewById(R.id.layout_wode);
        layoutHome.setOnClickListener(this);
        layoutWode.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.layout_home:
                imgHome.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.home_icon));
                txtHome.setVisibility(View.GONE);
                imgWode.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.wode_no));
                switchFragment(homeFragment).commit();
                break;
            case R.id.layout_wode:
                imgHome.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.home_icon_no));
                txtHome.setVisibility(View.VISIBLE);
                imgWode.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.wode));
                switchFragment(mineFragment).commit();
                break;
            default:
                break;
        }
    }

    private FragmentTransaction switchFragment(Fragment targetFragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!targetFragment.isAdded()) {
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            transaction.add(R.id.frameLayout, targetFragment, targetFragment.getClass().getName());
        } else {
            transaction.hide(currentFragment).show(targetFragment);
        }
        currentFragment = targetFragment;
        return transaction;
    }
}
